﻿//-----------------------------------------------------------------------
// <copyright file="$originalprojectname$Database.cs" company="$registeredorganization$">
// Copyright © $registeredorganization$
// </copyright>
//-----------------------------------------------------------------------

namespace $originalprojectname$.Service
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Security.Principal;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint.Utilities;

    /// <summary>
    /// Custom Database class. Contains logic to Provision/Upgrade/Remove the custom database.
    /// </summary>
    [System.Runtime.InteropServices.Guid("$databaseguid$")]
    internal sealed class $originalprojectname$Database : SPDatabase
    {
        #region Fields

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="$originalprojectname$Database"/> class. Default constructor (required for SPPersistedObject serialization). Do not call this directly.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public $originalprojectname$Database()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="$originalprojectname$Database"/> class. Use this constructor when creating a new database in the farm.
        /// </summary>
        /// <param name="databaseParameters">The database parameters to use when creating the new database.</param>
        internal $originalprojectname$Database(SPDatabaseParameters databaseParameters)
            : base(databaseParameters)
        {
            this.Status = SPObjectStatus.Disabled;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Contains the logic to Provision the database.
        /// </summary>
        public override void Provision()
        {
            // Stop if the DB has already been provisioned
            if (this.Status == SPObjectStatus.Online)
            {
                return;
            }

            // Update the status to provisioning
            this.Status = SPObjectStatus.Provisioning;
            this.Update();
            
            // Run the provisioning scripts if we are not attaching to an existing database
            if (!this.Exists)
            {$if$ ($spversion$ == 14)
                // Create any DB options
                Dictionary<string, bool> options = new Dictionary<string, bool>();
                options.Add(SPDatabase.SqlDatabaseOption[(int)DatabaseOptions.AutoClose], false);
                SPDatabase.Provision(
                    this.DatabaseConnectionString,
                    SPUtility.GetGenericSetupPath(@"TEMPLATE\SQL\$originalprojectname$.Service\CreateDatabase.sql"),
                    options);$endif$ $if$ ($spversion$ == 15)
                // Create any DB options
                Dictionary<DatabaseOptions, bool> options = new Dictionary<DatabaseOptions, bool>();
                options.Add(SPDatabase.DatabaseOptions.AutoClose, false);

                SPDatabase.Provision(
                    this.DatabaseConnectionString,
                    SPUtility.GetVersionedGenericSetupPath(@"TEMPLATE\SQL\$originalprojectname$.Service\CreateDatabase.sql", 15),
                    options);$endif$
            }

            this.Status = SPObjectStatus.Online;
            this.Update();
        }

        /// <summary>
        /// Grants the application pool service account rights to the database. Call this after you call Provision() 
        /// on this class.
        /// </summary>
        /// <param name="processSecurityIdentifier">The application pool service account.</param>
        internal void GrantApplicationPoolAccess(SecurityIdentifier processSecurityIdentifier)
        {
            this.GrantAccess(processSecurityIdentifier, "db_owner");
        }

        #endregion
    }
}
